#include "qremovedrivedialog.h"
#include "ui_qremovedrivedialog.h"

#include "../../lib/corelib/qcommondelegate.h"
#include "../../lib/shared/qproject.h"

#include <QTreeWidgetItem>

QRemoveDriveDialog::QRemoveDriveDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QRemoveDriveDialog),
    m_treeWidget(new QCommonTreeWidget(this))
{
    ui->setupUi(this);

    ui->horizontalLayout->addWidget(m_treeWidget,1);
    m_treeWidget->setFrameStyle(QFrame::Box);
    m_treeWidget->setRootIsDecorated(false);

    QProject *p=QProject::project();
    int count=p->getDriveCount();

    for(int i=0;i<count;i++)
    {
        tagDriveInfo info=p->getDrive(i);
        QTreeWidgetItem *item=new QTreeWidgetItem(m_treeWidget);
        item->setText(0,info.m_name);
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
        item->setCheckState(0,Qt::Unchecked);
        m_items.append(item);
    }
    connect(m_treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(itemClicked(QTreeWidgetItem*,int)));
}

QRemoveDriveDialog::~QRemoveDriveDialog()
{
    delete ui;
}

void QRemoveDriveDialog::on_okBtn_clicked()
{
    for(int i=0;i<m_items.size();i++)
    {
        if(m_items.at(i)->checkState(0)==Qt::Checked)
        {
            m_checked.append(m_items.at(i)->text(0));
        }
    }
    close();
}

void QRemoveDriveDialog::on_cancelBtn_clicked()
{
    close();
}

void QRemoveDriveDialog::itemClicked(QTreeWidgetItem *item, int column)
{
    if(item->checkState(0)==Qt::Unchecked)
    {
        item->setCheckState(0,Qt::Checked);
    }
    else
    {
        item->setCheckState(0,Qt::Unchecked);
    }
}

QList<QString> QRemoveDriveDialog::getChecked()
{
    return m_checked;
}
